package com.hschinese.pronunciation.utils;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import com.hschinese.pronunciation.bean.PlayAssetData;

import java.io.IOException;

public class HsSoundUtil {
	
	private static SoundPool soundPool = null;

	private static MediaPlayer mPlayer;
 	
	private HsSoundUtil(){
		
	}

	public static PlayAssetData playAsset(Context cxt, String name) {
		if (soundPool == null) {
			soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
		}
		final PlayAssetData mPlayAssetData = new PlayAssetData();
		AssetManager am = cxt.getAssets();
		mPlayAssetData.setSoundPool(soundPool);
		// 获取指定文件对应的AssetFileDescriptor
		try {
			AssetFileDescriptor afd = am.openFd(name);
			soundPool.load(afd, 0);
			soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
				@Override
				public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
					int id = soundPool.play(sampleId, 0.5f, 0.5f, 1, 0, 1);
					mPlayAssetData.setResId(id);
				}
			});
		} catch (IOException e) {
			e.printStackTrace();
		}
		return mPlayAssetData;
	}

	public static PlayAssetData playAssetNew(Context cxt, String name) {
		if (soundPool == null) {
			soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
		}
		if(mPlayer == null){
			mPlayer = new MediaPlayer();
		}
		final PlayAssetData mPlayAssetData = new PlayAssetData();
		mPlayAssetData.setSoundPool(soundPool);
		int duration = 0;
		AssetManager am = cxt.getAssets();
		try {
			AssetFileDescriptor afd = am.openFd(name);
//			mPlayer.setDataSource(afd.getFileDescriptor());
//			duration = mPlayer.getDuration();
//			mPlayAssetData.setDuration(duration);
			Logger.e("xionghy - duration: " + duration);
			soundPool.load(afd, 0);
			soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
				@Override
				public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
					int id = soundPool.play(sampleId, 0.5f, 0.5f, 1, 0, 1);
					mPlayAssetData.setResId(id);
				}
			});
		} catch (IOException e) {
			e.printStackTrace();
		}
		return mPlayAssetData;
	}
	
	/**
	 * 使用soundPool播放短的音频文件
	 * @param context
	 * @param resId
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static SoundPool play(Context context,int resId){
		if(soundPool == null){
			soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC,0);
		}
		soundPool.load(context,resId,0);
 		soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
			@Override
			public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
				soundPool.play(sampleId, 0.5f, 0.5f, 1, 0, 1);
			}
		});
		return soundPool;
	}

	/**
	 * 播放较长的音频文件
	 * @param context
	 * @param audioFileName 文件名称
	 * @return
	 */
	public static void play(String audioFileName){
		if(mPlayer == null){
	    	 mPlayer = new MediaPlayer();
	 		//播放完就reset()
	 		mPlayer.setOnCompletionListener(new OnCompletionListener() {
	 			@Override
	 			public void onCompletion(MediaPlayer mp) {
	 				 mp.release();
	 				 mp = null;
	 			}
	 		});
	 		mPlayer.setOnErrorListener(new OnErrorListener() {
	 			@Override
	 			public boolean onError(MediaPlayer mp, int what, int extra) {
	 				mp.release();
	 				mp = null;
	  				return false;
	 			}
	 		});
	    } 
	  try {
		    mPlayer.reset();
			mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
			mPlayer.setScreenOnWhilePlaying(true);
			mPlayer.setDataSource(audioFileName);
		    mPlayer.setVolume(0.5f,
				  1.0f);
 			mPlayer.prepareAsync();
 			mPlayer.setOnPreparedListener(new OnPreparedListener() {
 				@Override
 				public void onPrepared(MediaPlayer mp) {
 					mp.start();
 				}
 			}); 
 		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	
 	}
	
	/**
	 * release
	 * 释放MediaPlayer资源
	 * @param mediaPlayer
	 */
	public static void release( ){
		if(mPlayer != null){
			mPlayer.release();
			mPlayer = null;
		}
	}
	
	/**
	 * pause
	 * @param mediaPlayer
	 */
	public static void pause(){
		if(mPlayer != null)
			mPlayer.pause();
	}
	
	/**
	 * stop
	 * @param mediaPlayer
	 */
	public static void stop(){
		if(mPlayer != null)
			mPlayer.stop();
	}
}
